package com.qys.livesMall.shop.service.impl;


import com.qys.livesMall.app.user.model.entity.H2AppUser;
import com.qys.livesMall.app.user.service.IH2AppUserService;
import com.qys.livesMall.common.constants.Constants;
import com.qys.livesMall.common.message.MessageServer;
import com.qys.livesMall.common.utils.bean.BeanProUtils;
import com.qys.livesMall.finance.model.dto.AccountSearch;
import com.qys.livesMall.finance.model.entity.shop.FinanceShopAccount;
import com.qys.livesMall.finance.service.FinanceShopAccountService;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.Paging;
import com.qys.livesMall.framework.view.QysPage;
import com.qys.livesMall.shop.dao.H2ShopBalanceDao;
import com.qys.livesMall.shop.dao.H2ShopBalanceDetailDao;
import com.qys.livesMall.shop.dao.H2ShopBaseDao;
import com.qys.livesMall.shop.enums.ShopMessageEnum;
import com.qys.livesMall.shop.mapper.H2ShopBalanceMapper;
import com.qys.livesMall.shop.modal.dto.shop.balance.ShopBalanceHandle;
import com.qys.livesMall.shop.modal.dto.shop.balance.ShopBalanceSearch;
import com.qys.livesMall.shop.modal.entity.H2ShopBalance;
import com.qys.livesMall.shop.modal.entity.H2ShopBalanceDetail;
import com.qys.livesMall.shop.modal.entity.H2ShopBase;
import com.qys.livesMall.shop.modal.vo.ShopBalanceVO;
import com.qys.livesMall.shop.service.H2ShopBalanceDetailService;
import com.qys.livesMall.shop.service.H2ShopBalanceService;
import com.qys.livesMall.shop.service.ShopOrderSettlementInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 店铺余额表 服务实现类
 * </p>
 *
 * @author
 * @since 2023-06-06
 */
@Service
public class H2ShopBalanceServiceImpl implements H2ShopBalanceService {



    @Autowired
    private MessageServer messageServer;

    @Autowired
    private H2ShopBaseDao shopBaseDao;

    @Autowired
    private FinanceShopAccountService shopAccountService;

    @Autowired
    private IH2AppUserService userService;

    @Autowired
    private ShopOrderSettlementInfoService shopOrderSettlementInfoService;

    @Override
    public ShopBalanceVO get() {
        FinanceShopAccount balance=shopAccountService.get(SecurityUtils.getUserId());
        ShopBalanceVO vo=BeanProUtils.copyProperties(balance, ShopBalanceVO::new);
        //
        ShopBalanceVO vo2=shopOrderSettlementInfoService.getShopOrderWaitSettlementInfo(balance.getShopId());
        vo.setFreezeIn(BigDecimal.ZERO);
        vo.setFreezeOut(BigDecimal.ZERO);
        if (vo2==null){
            return vo;
        }
        vo.setFreezeOut(vo2.getFreezeOut()==null?BigDecimal.ZERO:vo2.getFreezeOut());
        vo.setFreezeIn(vo2.getFreezeIn()==null?BigDecimal.ZERO:vo2.getFreezeIn());
        vo.setTotalBalance(vo.getAvailableBalance().add(vo.getFreezeIn()).add(vo.getFreezeOut()));
        return vo;
    }


    @Override
    public QysPage<ShopBalanceVO> list(AccountSearch search) {
        H2AppUser user=null;
        if(search.getUserName()!=null){
            user=userService.getbyUserName(search.getUserName());
            if (user!=null){
                search.setUserId(Long.valueOf(user.getId()));
            }else {
                return QysPage.build(search);
            }
        }
        List<Long> shopIds =null;
        List<H2ShopBase> shops=null;
        if (StringUtils.isNotBlank(search.getShopName())) {
            shops = shopBaseDao.getByShopName(search.getShopName());
            if (CollectionUtils.isEmpty(shops)){
                return QysPage.build(search);
            }
            shopIds = shops.stream().map(H2ShopBase::getId).collect(Collectors.toList());
            search.setShopIds(shopIds);
        }
        QysPage<FinanceShopAccount> page=shopAccountService.list(search);
        if (CollectionUtils.isEmpty(page.getRecords())){
            return QysPage.build(search);
        }
        List<ShopBalanceVO> balanceVOS=null;
        shopIds = page.getRecords().stream().map(FinanceShopAccount::getShopId).collect(Collectors.toList());
        shops = shopBaseDao.listByIds(shopIds);
        balanceVOS=shopOrderSettlementInfoService.getShopOrderWaitSettlementInfos(shopIds);

        Map<Long, H2ShopBase> shopBaseMap = shops.stream().collect(Collectors.toMap(H2ShopBase::getId, v -> v));
        Map<Long, ShopBalanceVO> balanceVOMap=new HashMap<>();
        if (!CollectionUtils.isEmpty(balanceVOS)){
            balanceVOMap=balanceVOS.stream().collect(Collectors.toMap(ShopBalanceVO::getShopId, v -> v));
        }
        List<ShopBalanceVO> vos=new ArrayList<>();
        Map<Long, ShopBalanceVO> finalBalanceVOMap = balanceVOMap;
        page.getRecords().forEach(e->{
            ShopBalanceVO vo=BeanProUtils.copyProperties(e,ShopBalanceVO::new);
            H2ShopBase h2ShopBase=shopBaseMap.get(e.getShopId());
            if (null != h2ShopBase){
                vo.setShopName(h2ShopBase.getShopName());
            }
            ShopBalanceVO balanceVO= finalBalanceVOMap.get(e.getShopId());
            if (balanceVO!=null){
                vo.setFreezeIn(balanceVO.getFreezeIn());
                vo.setFreezeOut(balanceVO.getFreezeOut());
            }
            vo.setTotalBalance(vo.getAvailableBalance().add(vo.getFreezeIn()!=null?vo.getFreezeIn():BigDecimal.ZERO));
            vo.setTotalBalance(vo.getTotalBalance().add(vo.getFreezeOut()!=null?vo.getFreezeOut():BigDecimal.ZERO));
            vos.add(vo);
        });
        return QysPage.build(search).setRecords(vos).setTotal(page.getTotal());
    }

    @Override
    public List<FinanceShopAccount> listByUserIds(List<Long> userIds) {
        return shopAccountService.listByUserIds(userIds);
    }

    @Override
    public ShopBalanceHandle directIn(ShopBalanceHandle handle) {
        return null;
    }

    @Override
    public ShopBalanceHandle directOut(ShopBalanceHandle handle) {
        return null;
    }


}
